package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.Book;
import com.example.result.R;
import com.example.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Date 2022/9/6
 * @Description
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private IBookService bookService;

//    @GetMapping
//    public Result getAll(){
//        return new Result(bookService.list(), 0);
//    }

    @DeleteMapping("/{id}")
    public R deleteUser(@PathVariable Integer id) {
        boolean flag = bookService.removeById(id);
        return R.success(flag, "");
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public R getBooks(@PathVariable int currentPage, @PathVariable int pageSize) {
        IPage<Book> page = bookService.getPage(currentPage, pageSize, null);
        if (currentPage > page.getPages()) {
            page = bookService.getPage((int) page.getPages(), pageSize, null);
        }
        return R.success(page, "");
    }

}
